/*
 *Copyright 2011 Matthieu Paret
 *
 *This file is part of DragAndDrop.
 *
 *DragAndDrop is free software: you can redistribute it and/or modify
 *it under the terms of the GNU Lesser General Public License as published by
 *the Free Software Foundation, either version 3 of the License, or
 *(at your option) any later version.
 *
 *DragAndDrop is distributed in the hope that it will be useful,
 *but WITHOUT ANY WARRANTY; without even the implied warranty of
 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *GNU General Public License for more details.
 *
 *You should have received a copy of the GNU Lesser General Public License
 *along with DragAndDrop.  If not, see <http://www.gnu.org/licenses/>.
 */

package exp.mtparet.dragdrop;

import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import exp.mtparet.dragdrop.adapter.ItemAdapter;
import exp.mtparet.dragdrop.adapter.ReceverAdapter;
import exp.mtparet.dragdrop.data.OneItem;
import exp.mtparet.dragdrop.view.DragNDropListView;
import exp.mtparet.dragdrop.view.HorizontalListView;
import exp.mtparet.dragdrop.view.ListViewDragDrop;
import exp.mtparet.dragdrop.view.ListViewDragDrop.DragListener;
import exp.mtparet.dragdrop.view.ListViewDragDrop.DropListener;

public class DragAndDropListView extends Activity implements DragListener,
		DropListener {

	private RelativeLayout mainRelativeLayout;
	private ListViewDragDrop lvPicture;
	private ListViewDragDrop lvRecever;
	private ItemAdapter itemAdapter;
	private ReceverAdapter receverAdapter;
	private ImageView imageDrag;
	private OneItem oneItemSelected;
	private Context context;
	private boolean isDnd = false;
	private ArrayList<String> list = new ArrayList<String>();
	private ArrayList<OneItem> arrItem = new ArrayList<OneItem>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		context = this;
		list.add("http://www.wopll.com/");
		list.add("http://www.wopll.com/?p=368");
		list.add("http://www.wopll.com/?page_id=2");
		list.add("http://www.wopll.com/?feed=rss2");
		list.add("http://www.wopll.com/?cat=20");

		mainRelativeLayout = (RelativeLayout) RelativeLayout.inflate(context,
				R.layout.main_list, null);
		lvPicture = (ListViewDragDrop) mainRelativeLayout
				.findViewById(R.id.listView1);
		lvRecever = (ListViewDragDrop) mainRelativeLayout
				.findViewById(R.id.listView2);
		imageDrag = (ImageView) mainRelativeLayout
				.findViewById(R.id.imageView1);

		itemAdapter = new ItemAdapter(context, constructList());
		receverAdapter = new ReceverAdapter(context, constructList(),lvRecever);

		lvPicture.setAdapter(itemAdapter);
		lvRecever.setAdapter(receverAdapter);

		/**
		 * Set selected Listener to know what item must be drag
		 */
		lvPicture.setOnItemSelectedListener(mOnItemSelectedListener);

		/**
		 * Set an touch listener to know what is the position when item are move
		 * out of the listview
		 */
		lvPicture.setOnItemMoveListener(mOnItemMoveListener);

		/**
		 * Listener to know if the item is droped out of this origin ListView
		 */
		lvPicture.setOnItemUpOutListener(mOnItemUpOutListener);

		/**
		 * Listener to know on what position the new item must be insert
		 */
		lvRecever.setOnItemReceiverListener(listenerReceivePicture);

		lvPicture.setOnItemReceiverListener(listenerReceiveOwnPicture);

		/**
		 * An listemer to remove an item
		 */
		lvRecever.setOnItemLongClickListener(listenerRemoveItem);

		lvRecever.setDragListener(this);
		lvRecever.setDropListener(this);
		setContentView(mainRelativeLayout);
	}

	/**
	 * Save selected item
	 */
	private AdapterView.OnItemSelectedListener mOnItemSelectedListener = new AdapterView.OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {

			/**
			 * retrieve selected item from adapterview
			 */
			oneItemSelected = (OneItem) arg0.getItemAtPosition(arg2);
			imageDrag.setImageDrawable(context.getResources().getDrawable(
					oneItemSelected.getId()));

		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub

		}

	};

	private OnTouchListener mOnItemUpOutListener = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {

			return lvRecever.onUpReceive(event);
		}
	};

	private OnItemClickListener listenerReceivePicture = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
//			if (oneItemSelected != null)
//				receverAdapter.addPicture(oneItemSelected, arg2);

		}

	};
	private OnItemClickListener listenerReceiveOwnPicture = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			if (oneItemSelected != null)
				itemAdapter.addPicture(oneItemSelected, arg2);
		}
	};
	private OnItemLongClickListener listenerRemoveItem = new OnItemLongClickListener() {

		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
				int arg2, long arg3) {
//			receverAdapter.removeItem(arg2);
			return false;
		}

	};

	private OnTouchListener mOnItemMoveListener = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			RelativeLayout.LayoutParams layout = (RelativeLayout.LayoutParams) imageDrag
					.getLayoutParams();
			imageDrag.setVisibility(ImageView.VISIBLE);

			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				imageDrag.bringToFront();
				return true;
			}

			if (event.getAction() == MotionEvent.ACTION_MOVE) {
				layout.leftMargin = (int) event.getX() - imageDrag.getWidth()
						/ 2;
				layout.topMargin = (int) event.getY() - imageDrag.getHeight()
						/ 2;
			}

			if (event.getAction() == MotionEvent.ACTION_UP) {
				imageDrag.setVisibility(View.GONE);
			}

			imageDrag.setLayoutParams(layout);

			return true;
		}

	};

	private ArrayList<OneItem> constructList() {
		ArrayList<OneItem> al = new ArrayList<OneItem>();

		OneItem op = new OneItem(R.drawable.maison, "maison");
		al.add(op);

		OneItem op2 = new OneItem(R.drawable.dans, "dans");
		al.add(op2);

		OneItem op3 = new OneItem(R.drawable.dort, "dort");
		al.add(op3);

		OneItem op4 = new OneItem(R.drawable.garcon, "gar�on");
		al.add(op4);

		OneItem op5 = new OneItem(R.drawable.le, "le");
		al.add(op5);

		return al;
	}

	@Override
	public void drag(int from, int to) {
		// TODO Auto-generated method stub
		Log.e("adifhaidjflsjdflsd", "asdkjfhsdf");
		if (!isDnd) {
			isDnd = true;
			Log.i("Drag and Drop : drag", "from : " + from + ", to : " + to);
		}
	}

	@Override
	public void drop(int from, int to) {
		// TODO Auto-generated method stub
		Log.e("adfhalkdjfsd", "akdjfhslfdli ");
		if (isDnd) {
			Log.i("Drag and Drop : drop", "from : " + from + ", to : " + to);
			if (from == to)
				return;

			 OneItem item = arrItem.remove(from);
			 arrItem.add(to, item);
			isDnd = false;
			receverAdapter.drop(from, to);
			receverAdapter.notifyDataSetChanged();
			Log.e("rin here", "run hrer ");
			lvRecever.checkfordrop(to);
		}
	}
}
